package internationalenergytradingplatform.controller.SupplierController;

import internationalenergytradingplatform.VO.SupplierLoginVO;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.SupplierAccountDao;
import internationalenergytradingplatform.pojo.SupplierAccount;
import internationalenergytradingplatform.properties.JwtProperties;
import internationalenergytradingplatform.result.Result;
import internationalenergytradingplatform.service.SupplierAccountService;
import internationalenergytradingplatform.utils.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/supplier-account")
@Api(tags = "供应商账户相关类")
public class AccountController {

    @Autowired
    SupplierAccountService supplierAccountService;

    @Autowired
    JwtProperties jwtProperties;

    @Autowired
    SupplierAccountDao supplierAccountDao;

    /**
     *
     * @author yjh
     * @date 2024/2/13 21:14
     供应商登录
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/login")
    public Result<SupplierLoginVO> login(@RequestBody SupplierAccount supplierAccountDTO){
        log.info("员工登录：{}", supplierAccountDTO);
        SupplierAccount supplierAccount = supplierAccountService.login(supplierAccountDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("tele", supplierAccount.getTele());
        String token = JWTUtil.createJWT(
                jwtProperties.getAdminSecretKey(),//令牌的key
                jwtProperties.getAdminTtl(),//令牌的有效时间
                claims);

        //将供应商信息及token写入VO中
        SupplierLoginVO supplierLoginVO = SupplierLoginVO.builder()
                .tele(supplierAccount.getTele())
                .password(supplierAccount.getPassword())
                .companyName(supplierAccount.getCompanyName())
                .unifiedCreditCode(supplierAccount.getUnifiedCreditCode())
                .unifiedCreditImage(supplierAccount.getUnifiedCreditImage())
                .avatarImage(supplierAccount.getAvatarImage())
                .supplierAccountStatus(supplierAccount.getSupplierAccountStatus())
                .token(token)
                .build();
        log.info("登录结果：{}", supplierLoginVO);
        return Result.success(supplierLoginVO);
    }

    /**
     *
     * @author yjh
     * @date 2024/2/14 22:18
     供应商注册
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/register")
    public Result register(@RequestBody SupplierAccount supplierAccountDTO){
        log.info("员工注册：{}", supplierAccountDTO);
        Integer numberSuccessfulRegistrations = supplierAccountService.register(supplierAccountDTO);
        log.info("成功注册供应商{}人", numberSuccessfulRegistrations);
        return Result.success(numberSuccessfulRegistrations > 0);

    }

    /**
     *
     * @author yingfeng
     * @date 2024/4/25 22:09
     查询供应商余额
     */
    @CrossOrigin(origins = "*")
    @GetMapping("/banlance")
    public Result<Double> getBanlance(){
        String companyId = BaseContext.getCurrentId();//获取公司id
        Double banlance = supplierAccountDao.getBanlance(companyId);
        return Result.success(banlance);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/4/25 22:42
     供应商提现
     */
    @CrossOrigin(origins = "*")
    @PostMapping("withdrawalMoney/{money}")
    public Result withdrawalMoney(@PathVariable Double money){
        String companyId = BaseContext.getCurrentId();//获取公司id
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = now.format(formatter);
        //持久层提现
        supplierAccountDao.withdrawalMoney(formattedDateTime, money, companyId);
        return Result.success();
    }


}
